package com.example.za.account.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.za.account.R;
import com.example.za.account.application.AccountApplication;
import com.example.za.account.dao.UserDao;
import com.example.za.account.fragment.InvestFragment;
import com.example.za.account.fragment.HomeFragment;
import com.example.za.account.fragment.MineFragment;
import com.example.za.account.fragment.ReportFragment;
import com.example.za.account.model.User;
import com.example.za.account.utils.Constant;
import com.example.za.account.utils.T;

import cn.bmob.v3.BmobUser;

public class MainActivity extends BaseActivity {

//    private UserDao userDao;

    // 定义FragmentTabHost对象
    private FragmentTabHost mTabHost;
    // 定义一个布局
    private LayoutInflater layoutInflater;
    // 定义数组来存放Fragment界面
    private Class fragmentArray[] = {HomeFragment.class, InvestFragment.class, ReportFragment.class, MineFragment.class};
    // 定义数组来存放按钮图片
    private int mImageViewArray[] = {R.drawable.main_tab_item_home, R.drawable.main_tab_item_invest, R.drawable.main_tab_item_report, R.drawable.main_tab_item_mine};
    // Tab选项卡的文字
    private String mTextViewArray[] = {"记账", "投资", "报表", "我的"};
    private long exitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();


//        UserDao userDao = new UserDao(MainActivity.this);
//        List<User> users = userDao.findAllUser();
//        for (int i = 0 ;i < users.size();i++){
//
//            L.e( users.get(i).toString());
//        }

    }

    @Override
    protected Activity getSubActivity() {
        return this;
    }

    private void initView() {
        layoutInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        //调用setup方法进行FragmentManager和用来显示fragment的FrameLayout的绑定。
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        //切换顶部文字
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setTitle(tabId);
//                if (tabId == mTextViewArray[2]) {
//                    switchTitleBar(tabId, false, true, R.drawable.ic_expand_more);
//                } else {
//                    switchTitleBar(tabId, false, false, 0);
//                }
            }
        });
        // 得到fragment的个数
        int count = fragmentArray.length;
        for (int i = 0; i < count; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextViewArray[i])
                    .setIndicator(getTabItemView(i));
            // 调用addTab进行tab选项和fragment类进行绑定。
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            // 设置Tab按钮的背景
            mTabHost.getTabWidget().getChildTabViewAt(i).setBackgroundResource(R.drawable.main_tab_item_bg);
        }
        AccountApplication.sUser = new UserDao(this).getCurrentUser(BmobUser.getCurrentUser(User.class).getUsername());
        registerReceiver(mBroadcastReceiver, new IntentFilter(Constant.ACTION_FINISH));
    }

    /**
     * 给Tab按钮设置图标和文字
     *
     * @param i
     * @return
     */
    private View getTabItemView(int i) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[i]);
        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText(mTextViewArray[i]);
        return view;
    }

//    private void switchTitleBar(String title, boolean showBackward, boolean showForward, int forwardResId) {
//        setTitle(title);
//        if (showBackward) {
//            showBackwardView(true);
//        } else {
//            showBackwardView(false);
//        }
//        if (showForward) {
//            if (forwardResId == 0) {
//                showForwardView(true);
//            } else {
//                showForwardView(true, forwardResId);
//            }
//        } else {
//            showForwardView(false);
//        }
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > Constant.DELAY_TIME) {
                T.showShort(getApplicationContext(), R.string.text_exit);
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }


}
